/**
 * Created by Administrator on 2017/5/15.
 */

const initialState = {
  addedIds: [],
  quantityById: [],
  productId:[],
  data:{},
  shu:[]
}
const getdata= (state=initialState.data,action) =>{
  switch(action.type){
    case 'DATA':
      var {docs} = action.data
      return {...state,docs};
    default:
      return state;
  }
}
const addIds = (state = initialState, action) => {
  switch (action.type) {
    case 'ADD_PRODUCT':
      // for(var i = 0;i<state.productId.length;i++){
      //     state.shu.push(state.productId[i].productId)
      // }
      
      if (state.shu.indexOf(action.data.productId) !== -1) {
        //state.shu.splice(num,1)
        return state
      }else{
          state.shu.push(action.data.productId)
          return {...state,productId:[...state.productId,action.data]}
        }
      
    
    case "REM_COUNT":
      const num = state.shu.indexOf(action.data)
      state.productId.splice(num,1)
        // const id = action.data
        // for(var i=0;state.productId.length;i++){
        //     if(state.productId[i].productId == id){
        //         return state.productId.slice(i)
        //     }
        // }
      return state
    
    case "DEL_PRODUCT":
      var id = state.indexOf(action.productId)
      state.splice(id, 1)
      return state
    case "CLEAR_PRODUCT":
      state = []
      return state
    default:
      return state

  }
}
 const quantityId = (state = initialState.quantityById, action) => {
  switch (action.type) {
    case "ADD_PRODUCT":
      var {productId} = action.data
      return {
        ...state,
        [productId]: (state[productId] || 0) + 1
      };
    case "ADD_COUNT":
      var {data} = action
      return {
        ...state,
        [data]:state[data]+1
      }

    case "RED_COUNT":
      var {data} = action
      if(state[data]>1){
        return {
          ...state,
          [data]:state[data]-1
        }
      }

      
    
      case "DEL_COUNT":
      var {productId} = action
      if (state[productId] === 1) {
        delete state[productId]
        return {
          ...state
        }
      }
    
    
    case "DEL_COUNT":
      var {productId} = action
      if (state[productId] === 1) {
        delete state[productId]
        return {
          ...state
        }
      }
      return {
        ...state,
        [productId]: state[productId] - 1
      }
    case "CLEAR_PRODUCT":
      state = {}
      return state
    default:
      return state
  }
}
const shu1 = (state = initialState, action) => {
  switch (action.type) {
    case "REM_COUNT":
      const num = state.shu.indexOf(action.data)
      state.quantityById.splice(num,1)
      state.shu.splice(num,1)
      console.log(state,"ppppppp")
      console.log(num)
      return state
    default:
      return state
  }
}

const cart = (state = initialState, action) => {
  switch (action.type) {
    default:
      return {
        addIds: addIds(state.addIds, action),
        quantityId: quantityId(state.quantityId, action),
        getdata: getdata(state.data,action),
        shu1: shu1(state.shu1,action),
      }
  }
}
export default cart




